<?php


namespace app\admin\controller;

use app\admin\model\Notice as NoticeModel;
use my\Define;
use my\FTP;
use my\Security;
use my\Tools;
use think\Db;
use think\Request;

class Notice extends Base
{
    public function index()
    {
        $page = input('get.page');
        $limit = input('get.limit');
        if ($page && $limit) {
            $data = input('get.');
            $list = NoticeModel::getListInfo($page, $limit, $data);
            echo json_encode($list);
            exit;
        }
        return $this->fetch();
    }

    /**
     * 添加消息
     */
    public function add()
    {
        if (input('post.')) {
            //获取活动信息
            $data = [
                'is_top' => input('post.is_top'),        //页面是否显示
                'title' => input('post.title'),            //活动名称
                'main_title' => input('post.main_title'),            //活动名称
                'content' => input('post.content'),
                'create_time' => time(),                                     //添加时间
                'admin_id' => session('admin_id'),                 //添加人
            ];
            $data_post = input('post.');
            if (empty($data_post['target'])) {
                $this->error("请填写目标用户");
            }
            if ($data_post['phonearea']) {
                $data['uidas'] = explode(',', $data_post['phonearea']);
            }
            if (in_array(4, $data_post['target'])) {
                foreach ($data_post['phone'] as $key => $value) {
                    $cz = Db::name('driver_base')->where('driver_phone', Security::encrypt($value))->value('driver_id');
                    if ($cz) {
                        $data['uids2'][$key] = $cz;
                    }
                }
                foreach ($data['uidas'] as $key => $value) {
                    $cz = Db::name('driver_base')->where('driver_phone', Security::encrypt($value))->value('driver_id');
                    if ($cz) {
                        $data['uids1'][$key] = $cz;
                    }
                }
            }
            $data['uids2'] = implode(',', $data['uids2']);
            $data['uids1'] = implode(',', $data['uids1']);
            if ($data['uids2']){
                $data['uids'] = $data['uids2'] . "," . $data['uids1'];
            }else{
                $data['uids'] = $data['uids1'];
            }
            $data['driver_type'] = implode(',', $data_post['driver_type']);
            $data['target'] = implode(',', $data_post['target']);
            if ($data_post['time'] == 0) {
                $data['add_time'] = time();
            } else {
                $data['add_time'] = strtotime($data_post['add_time']);
            }
            if ($data_post['cs_type'] == 0) {
                $data['citys'] = 0;
            } else {
                $pid = [];
                foreach ($data_post['region'][2] as $key => $val) {
                    if (in_array($val, array_keys($data_post['region'][3]))) {

                    } else {
                        $data_post['region'][3][$val] = Db::name('region')->where('region_level', 2)->where('parent_id', $val)->column('region_id');
                    }
                }
                $data['citys'] = json_encode($data_post['region']);
            }

            Db::name('notice')->insertGetId($data);
            return "<script type='application/javascript'>parent.window.location.reload();</script>";
        } else {
            $provinces = Db::name('region')->where('region_level', 1)->where('region_id', 'not in', 33, 34, 35)->column('region_id,region_name');
            $this->assign('provinces', $provinces);
            $request = Request::instance();
            $domain = $request->domain();
            $this->assign('url', $domain . "/public/excel/电话号码.xls");
            $this->assign('url2', url('region'));
        }
        return $this->fetch();
    }

    //添加、编辑时----获取管理区域
    public function region()
    {
        $data_post = input('post.');
        $db = db('region');
        $map['c.parent_id'] = array('in', $data_post['pid']);
        $region = $db->alias('c')->join('region p', 'c.parent_id=p.region_id')
            ->where($map)->column('c.region_id,c.parent_id,c.region_name as child,p.region_name as parent');
        if (empty($region)) {
            return json(array('status' => 2, 'msg' => '没有相关子级区域'));
        }
        foreach ($region as $key => $val) {
            $pid = $val['parent_id'];
            unset($val['parent_id']);
            if ($data_post['level'] == 3) {
                $config['grand'][$pid] = $db->alias('c')->join('region p', 'c.parent_id=p.region_id')
                    ->where('c.region_id', 'in', $pid)->value('p.region_id');
            }
            $config['parents'][$pid] = $val['parent'];
            unset($val['parent']);
            $config['children'][$pid][] = $val;
        }
        $this->assign($config);
        return json(array('status' => 1, 'html' => $this->fetch()));
    }

    /**
     * 编辑版本
     * @param string
     * @param int
     * @return string//成功时返回
     */
    public function edit()
    {
        $id = input('get.id');
        if (input('post.')) {
            $data = [
                'is_top' => input('post.is_top'),        //页面是否显示
                'title' => input('post.title'),            //活动名称
                'main_title' => input('post.main_title'),            //活动名称
                'content' => input('post.content'),
                'create_time' => time(),                                     //添加时间
                'admin_id' => session('admin_id'),                 //添加人
            ];
            $data_post = input('post.');
            if ($data_post['phonearea']) {
                $data['uidas'] = explode(',', $data_post['phonearea']);
            }
            $uids1=array();
            $uids2=array();
            if (in_array(4, $data_post['target'])) {
                foreach ($data_post['phone'] as $key => $value) {
                    $cz = Db::name('driver_base')->where('driver_phone', Security::encrypt($value))->value('driver_id');
                    if ($cz) {
                        $uids2[$key] = $cz;
                    }
                }
                foreach ($data['uidas'] as $key => $value) {
                    $cz = Db::name('driver_base')->where('driver_phone', Security::encrypt($value))->value('driver_id');
                    if ($cz) {
                        $uids1[$key] = $cz;
                    }
                }
            }
            $uids2 = implode(',', $uids2);
            $uids1 = implode(',', $uids1);
            if ($uids2){
                $data['uids'] = $uids2 . "," . $uids1;
            }else{
                $data['uids'] = $uids1;
            }
            $data['driver_type'] = implode(',', $data_post['driver_type']);
            $data['target'] = implode(',', $data_post['target']);

            if ($data_post['time'] == 0) {
                $data['add_time'] = time();
            } else {
                $data['add_time'] = strtotime($data_post['add_time']);
            }
            $data_post = input('post.');
            if ($data_post['cs_type'] == 0) {
                $data['citys'] = 0;
            } else {
                $pid = [];
                foreach ($data_post['region'][2] as $key => $val) {
                    if (in_array($val, array_keys($data_post['region'][3]))) {

                    } else {
                        $data_post['region'][3][$val] = Db::name('region')->where('region_level', 2)->where('parent_id', $val)->column('region_id');
                    }
                }
                $data['citys'] = json_encode($data_post['region']);
            }

            Db::name('notice')->where('id', $id)->update($data);
            return "<script type='application/javascript'>alert('修改成功');parent.window.location.reload();</script>";
        } else {
            //查询活动信息
            $result = Db::name('notice')->where('id', $id)->find();
            $info['region'] = json_decode($result['citys'], true);
            $region = db('region');
            $config['pro_all'] = true;
            $config['edit_provinces'] = $region->where('region_id', 'in', $info['region'][2])->column('region_id,region_name');
            foreach ($info['region'][2] as $vp) {
                $config['edit_citys'][$vp] = $region->where('parent_id', $vp)->column('region_id,region_name');
            }
            /*管理市*/
            $map['region_name'] = array(array('eq', "市辖区"), array('like', "%县%"), array('like', "%市"), array('like', "%旗%"), 'or');
            foreach ($info['region'][3] as $key_ps => $val_ps) {
                $config['edit_city_ps'][$key_ps] = $region->where('region_id', 'in', $val_ps)->column('region_id,region_name');
                foreach ($val_ps as $v) {
                    $map['parent_id'] = $v;
                    $config['edit_countys'][$key_ps][$v] = $region->where($map)->column('region_id,region_name');
                }
                $config['city_all'][$key_ps] = true;
                $count_ct = $region->where('parent_id', $key_ps)->count();//被编辑人员对应省所管理的市级数
                if ($count_ct != count($val_ps)) {
                    $config['city_all'][$key_ps] = false;
                }
            }
            $provinces = Db::name('region')->where('region_level', 1)->where('region_id', 'not in', 33, 34, 35)->column('region_id,region_name');
            $phone = [];
            foreach (explode(',',$result['uids']) as $k => $v) {
                $phone[$k] = Security::decrypt(Db::name('driver_base')->where('driver_id',$v)->value('driver_phone'));
            }

            $phone=implode(',',$phone);
            $this->assign('provinces', $provinces);
            $this->assign('phone', $phone);
            $this->assign('config', $config);
            $this->assign('url', url('region'));
            $this->assign('info', $info);
            $this->assign('result', $result);
            return $this->fetch();
        }
    }

    public function del($id)
    {
        NoticeModel::where('id', $id)->delete();
        return ['status' => 1, 'msg' => '删除成功'];
    }
}